package com.feign.feignClient;

import com.feign.feignClient.client.*;
import com.feign.feignClient.coder.MyDecoder;
import com.feign.feignClient.coder.MyEncoder;
import com.feign.feignClient.contract.MyContract;
import com.feign.feignClient.entity.Person;
import com.feign.feignClient.entity.UserInfo;
import feign.Feign;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.jaxb.JAXBContextFactory;
import feign.jaxb.JAXBDecoder;
import feign.jaxb.JAXBEncoder;
import feign.jaxrs.JAXRSContract;


/**
 * 测试Http请求，必须启动当前项目的SpringBoot启动类，才能运行成功
 */
public class Test1 {

	public static void main(String[] args) {
		testHello();
		System.out.println( );
		testPerson();
		System.out.println( );
		testUserInfo();
		System.out.println( );
		testCoder();
		System.out.println( );
		testClient();
	}


	public static void testHello(){
		System.out.println(" -----test hello AND test QueryStringParam（application/x-www-form-urlencoded）------ ");
		// 使用HelloClient调用HelloController的映射路径/hello
		HelloClient hello = Feign.builder().target(HelloClient.class,
				"http://localhost:8080/");
		System.out.println(hello.getClass().getName());
		System.out.println(hello.sayHello());
		System.out.println(hello.list(2L));
	}




	public static void testPerson(){
		System.out.println(" -----test person(json调用方式)------ ");
		// 使用PersonClient调用HelloController的映射路径,decoder为解码器
		PersonClient personClient = Feign.builder().decoder(new GsonDecoder()).
				target(PersonClient.class, "http://localhost:8080/");
		//PUT操作
		System.out.println(personClient.getPerson(2L));
		//POST操作
		Person p = new Person();
		p.setName("test");
		p.setId(5L);
		//这里使用encode编码器对Person对象进行json格式编码
		personClient = Feign.builder().encoder(new GsonEncoder()).
				target(PersonClient.class, "http://localhost:8080/");
		System.out.println(personClient.personSaveJson(p));

	}

	public static void testUserInfo(){
		System.out.println(" -----test userInfo(xml调用方式)------ ");
		JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder().build();
		//POST操作
		UserInfo userInfo = new UserInfo();
		userInfo.setName("test");
		userInfo.setId(5L);
		//这里使用encode编码器对UserInfo对象进行XML格式编码
		UserClient user = Feign.builder().
				encoder(new JAXBEncoder(jaxbFactory)).
				decoder(new JAXBDecoder(jaxbFactory)).
				target(UserClient.class, "http://localhost:8080/");
		System.out.println(user.userSaveXml(userInfo));

	}



	public static void testCoder(){
		System.out.println(" -----test 自定义Encoder Dencoder（FastJson方式）------ ");
		PersonClient personClient = Feign.builder().decoder(new MyDecoder()).
				target(PersonClient.class, "http://localhost:8080/");
		System.out.println(personClient.getPerson(2L));
		Person p = new Person();
		p.setName("test");
		p.setId(5L);
		personClient = Feign.builder().encoder(new MyEncoder()).
				target(PersonClient.class, "http://localhost:8080/");
		System.out.println(personClient.personSaveJson(p));
	}


	public static void testClient(){
		System.out.println(" -----test 自定义Client（HttpClient方式）------ ");
		System.out.println(" -----test hello------ ");
		HelloClient hello = Feign.builder().client(new MyFeignClient()).target(HelloClient.class,
				"http://localhost:8080/");
		System.out.println(hello.getClass().getName());
		System.out.println(hello.sayHello());

	}



}
